package NO6_Tree;

/**
 * 2025-08-20
 * 树节点类
 */
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public static void printTreeSimple(TreeNode root) {
        if (root == null) return;
        printTreeSimple(root.left);
        System.out.print(root.val + " ");
        printTreeSimple(root.right);
    }

    public static void printTree(TreeNode root) {
        if (root == null) {
            System.out.println("Tree is empty");
            return;
        }
        System.out.println("The Tree:");
        printTree(root, "", true, true);
    }

    private static void printTree(TreeNode node, String prefix, boolean isTail, boolean isLeft) {
        if (node == null) {
            return;
        }

        System.out.println(prefix + (isTail ? (isLeft ? "└── L: " : "└── R: ") : (isLeft ? "├── L: " : "├── R: ")) + node.val);

        if (node.left != null || node.right != null) {
            String newPrefix = prefix + (isTail ? "    " : "│   ");
            if (node.right != null) {
                printTree(node.right, newPrefix, false, false);
            }
            if (node.left != null) {
                printTree(node.left, newPrefix, true, true);
            }
        }
    }
}